import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () =>
        import('@/views/dashboard/index'),
      meta: {
        title: '主页',
        icon: 'dashboard'
      }
    }]
  },

  {
    path: '/auth',
    component: Layout,
    redirect: '/authority/account',
    name: 'Example',
    meta: {
      title: '权限管理',
      icon: 'el-icon-s-help'
    },
    children: [{
        path: 'user',
        name: 'user',
        component: () => import('@/views/authority/account/index'),
        meta: {
          title: '账号管理',
          icon: 'table'
        }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/authority/role/index'),
        meta: {
          title: '角色管理',
          icon: 'tree'
        }
      },
      {
        path: 'permission',
        name: 'permission',
        component: () => import('@/views/authority/resource/index'),
        meta: {
          title: '资源管理',
          icon: 'el-icon-s-fold'
        }
      }
    ]
  },


  {
    path: '/product',
    component: Layout,
    redirect: '/goods/menu1',
    name: 'Nested',
    meta: {
      title: '商品管理',
      icon: 'el-icon-s-goods'
    },
    children: [{
        path: 'list',
        component: () => import('@/views/goods/goodsList/index'), // Parent router-view
        name: 'list',
        meta: {
          title: '商品列表',
          icon: 'el-icon-goods'
        },
      },
      {
        path: 'category',
        component: () => import('@/views/goods/goodsSort/index'),
        name: 'category',
        meta: {
          title: '商品分类',
          icon: 'el-icon-menu'
        }
      },
      {
        path: 'brand',
        component: () => import('@/views/goods/brand/index'), // Parent router-view
        name: 'brand',
        meta: {
          title: '品牌管理',
          icon: 'el-icon-s-shop'
        },
      },
      {
        path: 'addDetail',
        component: () => import('@/views/goods/addGoods/index'),
        name: 'addDetail',
        hidden: true,
        meta: {
          title: '新增商品',
          icon: 'el-icon-s-promotion',
          activeMenu: '/product/list'
        }
      },
      {
        path: 'address',
        component: () => import('@/views/order/address/index'),
        name: 'address',
        meta: {
          title: '地址列表',
          icon: 'el-icon-office-building'
        }
      },
    ]
  },


  {
    path: '/order',
    component: Layout,
    redirect: '/order/list',
    name: 'order',
    meta: {
      title: '订单管理',
      icon: 'el-icon-document'
    },
    children: [{
        path: 'list',
        component: () => import('@/views/order/menu1/index'), // Parent router-view
        name: 'list',
        meta: {
          title: '订单列表',
          icon: 'el-icon-document-checked'
        },
      },
      {
        path: 'back',
        component: () => import('@/views/order/backGoods/index'),
        name: 'back',
        meta: {
          title: '退单列表',
          icon: 'el-icon-document-delete'
        }
      },
      {
        path: 'backDetail',
        component: () => import('@/views/order/backInfo/index'),
        name: 'backDetail',
        hidden: true,
        meta: {
          title: '退单详细',
          icon: 'el-icon-office-building',
          activeMenu: '/order/back'
        }
      },
      {
        path: 'detail',
        component: () => import('@/views/order/orderInfo/index'),
        name: 'detail',
        hidden: true,
        meta: {
          title: '订单详细',
          icon: 'el-icon-office-building',
          activeMenu: '/order/list'
        }
      }
    ]
  },

  {
    path: '/user',
    component: Layout,
    redirect: '/user/menu1',
    name: 'user',
    meta: {
      title: '注册用户管理',
      icon: 'el-icon-user-solid'
    },
    children: [{
      path: 'list',
      component: () => import('@/views/user/menu1/index'), // Parent router-view
      name: 'list',
      meta: {
        title: '用户列表',
        icon: 'el-icon-user'
      },
    }]
  },


  {
    path: '/market',
    component: Layout,
    redirect: '/marketing/menu1',
    name: 'market',
    meta: {
      title: '营销管理',
      icon: 'el-icon-data-analysis'
    },
    children: [{
        path: 'list',
        component: () => import('@/views/marketing/menu1/index'), // Parent router-view
        name: 'list',
        meta: {
          title: '营销列表',
          icon: 'el-icon-takeaway-box'
        },
      },
      {
        path: 'advertisement',
        component: () => import('@/views/marketing/menu2/index'), // Parent router-view
        name: 'advertisement',
        meta: {
          title: '广告列表',
          icon: 'el-icon-document-copy'
        },
      }
    ]
  },

  {
    path: '/content',
    component: Layout,
    redirect: '/content/menu1',
    name: 'content',
    meta: {
      title: '内容管理',
      icon: 'el-icon-copy-document'
    },
    children: [{
        path: 'articleList',
        component: () => import('@/views/content/menu1/index'), // Parent router-view
        name: 'articleList',
        meta: {
          title: '文章列表',
          icon: 'el-icon-tickets'
        },
      },
      {
        path: 'materialList',
        component: () => import('@/views/content/menu2/index'), // Parent router-view
        name: 'materialList',
        meta: {
          title: '素材列表',
          icon: 'el-icon-picture-outline'
        },
      },
      {
        path: 'addArticle',
        component: () => import('@/views/content/addArticle/index'), // Parent router-view
        name: 'addArticle',
        hidden: true,
        meta: {
          title: '新增文章',
          icon: 'el-icon-picture-outline',
          activeMenu: '/content/articleList'
        },
      },
      {
        path: 'editArticle',
        component: () => import('@/views/content/editArticle/index'), // Parent router-view
        name: 'editArticle',
        hidden: true,
        meta: {
          title: '编辑文章',
          icon: 'el-icon-picture-outline',
          activeMenu: '/content/articleList'
        },
      }
    ]
  },



  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
